package com.lims.algorithm.algset.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * <功能简述>二叉树层序遍历，每一层独立打印，之字形打印
 *
 * @author lims
 * @date 2020-09-14 10:00
 */
public class LevelPrintBinTree {
    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    /**
     * 层序遍历
     */
    public List<Integer> levelIter(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();
            result.add(node.val);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        return result;
    }

    /**
     * 层序遍历 分层
     */
    public List<List<Integer>> levelStratified(TreeNode root) {
        LinkedList<List<Integer>> result = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            List<Integer> list = new LinkedList<>();
            int count = queue.size();
            for (int i = 0; i < count; ++i) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            result.add(list);
        }
        return result;
    }

    /**
     * 之字形打印，第一层左右，第二层右左，第三层左右... 依次
     */
    public List<List<Integer>> levelZhi(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            LinkedList<Integer> tmp = new LinkedList<>();
            int count = queue.size();
            for (int i = 0; i < count; ++i) {
                TreeNode node = queue.poll();
                if (res.size() % 2 == 0) {
                    tmp.addLast(node.val);
                } else {
                    tmp.addFirst(node.val);
                }
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            res.add(tmp);
        }
        return res;
    }

    /************************************/
    public static void main(String[] args) {
        /**
         * *       3
         * *     /   \
         * *    9    20
         * *        /  \
         * *       15  7
         */
        TreeNode node1 = new TreeNode(3);
        TreeNode node2 = new TreeNode(9);
        TreeNode node3 = new TreeNode(20);
        TreeNode node4 = new TreeNode(15);
        TreeNode node5 = new TreeNode(7);

        node1.left = node2;
        node1.right = node3;
        node3.left = node4;
        node3.right = node5;

        LevelPrintBinTree tree = new LevelPrintBinTree();
        System.out.println("层序遍历：");
        System.out.println(tree.levelIter(node1));
        System.out.println("层序遍历分层：");
        System.out.println(tree.levelStratified(node1));
        System.out.println("层序遍历之字：");
        System.out.println(tree.levelZhi(node1));
    }
}










